/**
 * Created by wxb on 2019/06/30.
 */
(function () {
  'use strict';

  angular.module('BlurAdmin.pages.hotelManage.flightRecord')
    .controller('FlightListPageCtrl', FlightListPageCtrl);

  /** @ngInject */
  function FlightListPageCtrl($scope, hotelManageService, toastr, $uibModal, PAGE_SIZE, localStorageService) {
    $scope.PAGE_SIZE = PAGE_SIZE;
    $scope.companyType = localStorageService.cookie.get('user').companyType;

    var fDate;

    $scope.filterDate = function(date) {
      fDate = date;
      $scope.callServer();
    };

    $scope.callServer = function (tableState, ctrl) {
      if ( !$scope.stCtrl && ctrl ) {
        $scope.stCtrl = ctrl;
      }
      if( !tableState && $scope.stCtrl ) {
        $scope.stCtrl.pipe();
        return;
      }
      console.log(tableState);

      $scope.isLoading = true;

      var params = {};
      angular.copy(tableState.search.predicateObject, params);

      if (fDate) {
        params.originDate = fDate;
      }

      params.pageNo = tableState.pagination.start/20 + 1 || 1;
      params.pageSize = tableState.pagination.number || PAGE_SIZE;
      console.log(params);
      hotelManageService.flightList(params).then(function (data) {
        if (data && data.errcode === 0) {
          $scope.data = data.data;
          tableState.pagination.numberOfPages = data.data.totalPage;
          $scope.isLoading = false;
        }
      });
    };

    $scope.setItem = function (item) {
      var formData = {};
      angular.copy(item, formData);
      var modalInstance = $uibModal.open({
        animation: true,
        templateUrl: 'app/pages/hotelManage/flightRecord/flightList/modal/setFlight.html',
        controller: 'SetFlightModalCtrl',
        resolve: {
        }
      });

      modalInstance.result.then(function (modalItem) {
        hotelManageService.auditFlight(modalItem, {recordId: item.id}).then(function (data) {
          if (data && data.errcode === 0) {
            toastr.success('操作成功', '成功!');
            $scope.callServer();
          }
        })


      });
    };


  }
})();
